<?php
//require section
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_document.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
$title = 'Admin Panel';


$id = 0;
$email = "";
$parent_id = "";
$action = "Add";
$action_show = "Thêm";
if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_document::get_document_category_by_id($id);
    $id = $edit_item['cat_id'];
    $email = $edit_item['cat_name'];
    $parent_id = $edit_item['cat_parent_id'];
    $action = "Update";
    $action_show = "Sửa";
} else if (!empty($_POST)) {

    $id_save = "";
    if (!empty($_POST['hidd_id'])) {
        $id_save = $_POST['hidd_id'];
    }
    $first_name = "";
    if (!empty($_POST['txt_name'])) {
        $first_name = $_POST['txt_name'];
    }
    $parent_id_save = "";
    if (!empty($_POST['ddl_parent_id'])) {
        $parent_id_save = $_POST['ddl_parent_id'];
    }

    if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {

        $error_show = api_document::validate_category_fields($first_name);
        if (empty($error_show)) {
            if (api_document::save_document_category($id_save, $first_name, $parent_id_save)) {
                $success_info = "Thêm loai tài liệu &lt;".$first_name."&gt; thành công.";
                if ($id_save > 0) {
                    $success_info = "Sửa loai tài liệu &lt;".$first_name."&gt; thành công.";
                }
            }
        } else {
            $id = $id_save;
            $email = $first_name;
            $parent_id = $parent_id_save;
            $action = $_POST['Action'];
            $action_show = "Sửa";
            if ($action === "Add") {
                $action_show = "Thêm";
            }
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {

        $id_delete = 0;
        if (!empty($_POST['delete_id'])) {
            $id_delete = $_POST['delete_id'];
        }
        api_document::delete_document_category($id_delete);
        $success_info = "Xóa loại tài liệu thành công.";
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="uk-container uk-container-center uk-margin-top">
            <?php
            require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
            ?>

            <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">
                    <form action="?" method="POST" id="form_delete" >
                        <input type="hidden" name="delete_id" value="0" />
                        <input type="hidden" name="Action" value="Delete" />
                    </form>
                    <form name="form_data" id="form_data" method="POST"
                          action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
                          enctype="multipart/form-data"
                          class="uk-form uk-width-medium-1-1">
                    <?php
                    if (!empty($error_show)) {?>
                    <div class="error">Lỗi: <?php echo $error_show; ?></div>
                    <?php
                    } else if (!empty($success_info)) {?>
                    <div class="success"><?php echo $success_info; ?></div>
                    <?php
                    }?>
                <fieldset>
                    <legend>Danh mục bài tập</legend>
                    <input type="hidden" name="hidd_id" value="<?php echo $id; ?>" />

                    <div class="uk-form-row">
                        <label>Tên loại</label>
                        <input type="text" name="txt_name" value="<?php echo $email; ?>" placeholder="Text input">
                    </div>

                    <div class="uk-form-row">
                        <label>Loại tài liệu cấp trên</label>
                        <select name="ddl_parent_id">
                            <option value="0">-</option>
                            <?php
                            $list = api_document::get_all_document_categories();

                            foreach ($list as $i => $item) {
                                ?>
                                <option value="<?php echo $item['cat_id'] ?>"
                                <?php
                                if (!empty($parent_id)) {

                                    if ($item['cat_id'] == $parent_id) {
                                        echo 'selected';
                                    }
                                }
                                ?>
                                        >
                                    <?php echo $item['cat_name'] ?> </option>
                            <?php

                            }

                            ?>
                        </select>
                    </div>

                    <div class="uk-form-row">
                        <input type="hidden" name="Action" value="<?php echo $action; ?>">
                        <button class="uk-button uk-button-primary" id="action-button" onclick="submit_data();"><?php echo $action_show; ?></button>
                        <a href="?">Hủy</a>
                    </div>
                </fieldset>
                </form>
                <hr/>
                <h3><strong>Danh sách</strong></h3>
                <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
                                <thead>
                                    <tr>
                                        <th>Tên loại</th>
                                        <th>Loại tài liệu cấp trên</th>
                                        <th>Thao tác</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php

                                    $current_page = 1;
                                    $page_size = 10;
                                    if (!empty($_GET['p'])) {
                                        $current_page = $_GET['p'];
                                    }

                                    $list_total = api_document::get_all_document_categories();
                                    $lib_pager = new lib_pager();
                                    $total_page = $lib_pager->get_total_page($list_total, $page_size);
                                    $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                                    $count = 0;
                                    if (empty($current_page_list) || count($current_page_list) == 0) {
                                        echo "<tr><td>Chưa có dữ liệu</td></tr>";
                                    } else {
                                        foreach ($current_page_list as $i => $item) {?>
                                        <tr>
                                            <td><?php echo $item['cat_name'] ?></td>
                                            <td><?php echo $item['cat_parent_name'] ?></td>
                                            <td>
                                                <a href="?Action=Edit&Id=<?php echo $item['cat_id']; ?>">
                                                <i class="uk-icon-edit"></i>
                                                </a>
                                                <i class="uk-icon-eraser" onclick="confirmDelete('<?php echo $item['cat_id']; ?>')"></i>
                                            </td>
                                        </tr>
                                        <?php
                                        }
                                    } ?>
                                    <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                        <td colspan="4">
                                            <table>
                                                <tbody>
                                                    <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                                        <td colspan="4">
                                                            <table>
                                                                <tbody>
                                                                    <tr>
                                                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                                                            </td>
                                                                        <?php } ?>
                                                                    </tr>
                                                                </tbody>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

            </section>



            <?php
            require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
            ?>
        </div>
    </body>
</html>